﻿using System;
using Blackbird.Common.Utility;
using Blackbird.Core.Editable.Base;
using Blackbird.Studio.ViewModel.EntityViewModels;

namespace Blackbird.Studio.Context {
    public static class EntityModelContext {
        
        public static TModel CreateModel<TModel>(string fileName) where TModel : IEditableEntity
        {
            return SerializationUtility.CreateFromFile<TModel>(fileName);
        }

        public static TModel CreateModel<TModel>() where TModel : IEditableEntity {
            var model = Activator.CreateInstance<TModel>();
            model.Name = string.Format(Common.Properties.Resources.UntitledEntityFormat, model.TypeName);
            model.Initialize();
            return model;
        }

        public static TViewModel CreateViewModel<TViewModel, TModel>()
            where TViewModel : EntityViewModel<TModel>
            where TModel : IEditableEntity {
            var model = CreateModel<TModel>();
            
            return CreateViewModel<TViewModel, TModel>(model);
        }

        public static TViewModel CreateViewModel<TViewModel, TModel>(TModel model)
            where TViewModel : EntityViewModel<TModel>
            where TModel : IEditableEntity {
            
            var viewModel = Activator.CreateInstance(typeof(TViewModel), model);
            ((EntityViewModel<TModel>)viewModel).Initialize();

            return (TViewModel)viewModel;
        }

        public static TViewModel CreateViewModel<TViewModel, TModel>(string filename)
            where TViewModel : EntityViewModel<TModel>
            where TModel : IEditableEntity
        {
            return CreateViewModel<TViewModel, TModel>(CreateModel<TModel>(filename));
        }

    }
}
